package com.yupi.springbootinit.getOpenAi;

import cn.hutool.http.HttpRequest;
import cn.hutool.json.JSONUtil;
import com.yupi.springbootinit.common.ErrorCode;
import com.yupi.springbootinit.exception.BusinessException;
import org.apache.commons.lang3.StringUtils;

/**
 * openai 对话示例
 */
public class OpenAi  {

    public CreateCharCompletionResponse createCharCompletionResponse(CreateCharCompletionResponse request,String openAiApiKey){

        if (StringUtils.isBlank(openAiApiKey)){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"未传 openAiApiKey");
        }
        String url = "";
        //这里是一个map
        String json = JSONUtil.toJsonStr(request);
        String result = HttpRequest.post(url)
                .header("Authorization","Bearer" + openAiApiKey)
                .body(json)
                .execute()
                .body();
        return JSONUtil.toBean(result,CreateCharCompletionResponse.class);


    }
}
